package com.carol.lmsbe.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carol.lmsbe.common.Result;
import com.carol.lmsbe.dto.ConsumableApprovalRequest;
import com.carol.lmsbe.dto.ConsumableConsumptionDTO;
import com.carol.lmsbe.dto.ConsumableReceiveCreateRequest;
import com.carol.lmsbe.dto.ConsumableReceiveDTO;
import com.carol.lmsbe.dto.ConsumableReceiveQueryRequest;
import com.carol.lmsbe.service.ConsumableReceiveService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 易耗品领用控制器
 */
@RestController
@RequestMapping("/api/consumable-receives")
public class ConsumableReceiveController {

    @Autowired
    private ConsumableReceiveService consumableReceiveService;

    /**
     * 分页查询易耗品领用记录
     */
    @GetMapping("/list")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER', 'LAB_MANAGER')")
    public Result<Page<ConsumableReceiveDTO>> listReceives(ConsumableReceiveQueryRequest request) {
        Page<ConsumableReceiveDTO> page = consumableReceiveService.listReceives(request);
        return Result.success(page);
    }

    /**
     * 根据ID获取易耗品领用记录详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER', 'LAB_MANAGER')")
    public Result<ConsumableReceiveDTO> getReceiveById(@PathVariable Long id) {
        ConsumableReceiveDTO receive = consumableReceiveService.getReceiveById(id);
        return Result.success(receive);
    }

    /**
     * 创建易耗品领用记录
     */
    @PostMapping
    @PreAuthorize("hasAnyRole('ADMIN', 'LAB_MANAGER')")
    public Result<ConsumableReceiveDTO> createReceive(@Valid @RequestBody ConsumableReceiveCreateRequest request) {
        ConsumableReceiveDTO receive = consumableReceiveService.createReceive(request);
        return Result.success(receive);
    }

    /**
     * 更新易耗品领用记录
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'LAB_MANAGER')")
    public Result<ConsumableReceiveDTO> updateReceive(@PathVariable Long id, @Valid @RequestBody ConsumableReceiveCreateRequest request) {
        ConsumableReceiveDTO receive = consumableReceiveService.updateReceive(id, request);
        return Result.success(receive);
    }

    /**
     * 删除易耗品领用记录
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'LAB_MANAGER')")
    public Result<Void> deleteReceive(@PathVariable Long id) {
        consumableReceiveService.deleteReceive(id);
        return Result.success();
    }

    /**
     * 审批易耗品领用申请
     */
    @PostMapping("/{id}/approve")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER')")
    public Result<Void> approveReceive(@PathVariable Long id, @Valid @RequestBody ConsumableApprovalRequest request) {
        consumableReceiveService.approveReceive(id, request.getStatus(), request.getComment());
        return Result.success();
    }

    /**
     * 获取我的易耗品领用记录
     */
    @GetMapping("/my-receives")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER', 'LAB_MANAGER')")
    public Result<Page<ConsumableReceiveDTO>> getMyReceives(ConsumableReceiveQueryRequest request) {
        Page<ConsumableReceiveDTO> page = consumableReceiveService.getMyReceives(request);
        return Result.success(page);
    }

    /**
     * 获取未购买物品列表
     */
    @GetMapping("/unpurchased")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER', 'LAB_MANAGER')")
    public Result<Page<ConsumableReceiveDTO>> getUnpurchasedItems(ConsumableReceiveQueryRequest request) {
        Page<ConsumableReceiveDTO> page = consumableReceiveService.getUnpurchasedItems(request);
        return Result.success(page);
    }

    /**
     * 获取消耗情况统计
     */
    @GetMapping("/consumption-statistics")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER', 'LAB_MANAGER')")
    public Result<Page<ConsumableReceiveDTO>> getConsumptionStatistics(ConsumableReceiveQueryRequest request) {
        Page<ConsumableReceiveDTO> page = consumableReceiveService.getConsumptionStatistics(request);
        return Result.success(page);
    }

    /**
     * 获取消耗查询数据（包含计划vs实际对比）
     */
    @GetMapping("/consumption-query")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER', 'LAB_MANAGER')")
    public Result<Page<ConsumableConsumptionDTO>> getConsumptionQuery(ConsumableReceiveQueryRequest request) {
        Page<ConsumableConsumptionDTO> page = consumableReceiveService.getConsumptionQuery(request);
        return Result.success(page);
    }
}
